package com.scs.application.modules.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.modules.base.entity.MaterialBidCert;
import com.scs.application.modules.base.request.MaterialBidCertQueryRequest;
import com.scs.application.modules.base.service.MaterialBidCertService;
import com.scs.application.modules.base.vo.MaterialBidCertVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 耗材报价资质 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/material_bid_certs")
public class MaterialBidCertController extends CrudController<MaterialBidCert, String, MaterialBidCertService> implements ImAndExAbleController<MaterialBidCertQueryRequest> {


    @GetMapping("/page")
    public PageResult page(MaterialBidCertQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(MaterialBidCertQueryRequest request) {
        List<MaterialBidCert> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 报价单明细资质查询
     */
    @GetMapping("/listItem")
    public CommonResult listItem(MaterialBidCertQueryRequest request) {
        List<MaterialBidCertVO> list = service.listItem(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 添加资质
     *
     * @param entity
     * @return
     */
    @PostMapping("/save_for_materialBidCert")
    public CommonResult saveForMaterialBid(@RequestBody @Valid MaterialBidCert entity) {
        if (entity.getDocIds().size() == 0) {
            return CommonResult.error("资质文件至少上传一个");
        }
        boolean bSave = service.saveForMaterialBid(entity);
        return bSave ? CommonResult.ok("保存成功", entity) : CommonResult.error("保存失败");
    }

    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<MaterialBidCert> children) {
        if (children.isEmpty()) {
            return CommonResult.error("保存失败");
        }
        boolean bSuccess = service.saveOrUpdateBatch(children);
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }

}
